﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using KendoMvcUI.UI;
using KendoMvcUI.UI.Grid;

namespace KendoMvcUI.Core
{
    /// <summary>
    /// HtmlHelper 帮助类
    /// </summary>
    public static class UIGenerateFactory
    {
        /// <summary>
        /// 在同一Page中（URL相同）维护一个UIEx
        /// </summary>
        public static Dictionary<string, UIEx> _UIExDictionary = new Dictionary<string, UIEx>();
        /// <summary>
        /// Html在Razor中调用的总入口
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static UIEx UI(this HtmlHelper helper)
        {
            //if (helper.ViewContext.HttpContext == null)
            //    return new UIEx(helper.ViewContext, helper.ViewDataContainer);
            //string requestUrl = helper.ViewContext.HttpContext.Request.Url.AbsolutePath.ToLower();
            //if (!_UIExDictionary.ContainsKey(requestUrl))
            //    _UIExDictionary.Add(requestUrl, new UIEx(helper.ViewContext, helper.ViewDataContainer));
            //return _UIExDictionary[requestUrl];
            return new UIEx(helper.ViewContext,helper.ViewDataContainer);
        }
    }
    /// <summary>
    /// 生成UI对应的类
    /// </summary>
    public class UIEx
    {
        private string _jqueryScript = @"<script>
                                            $(function(){
                                                {0}
                                            });
                                        </script>";
        /// <summary>
        /// 根据类型生成对应的Javascript
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1"></typeparam>
        /// <param name="action"></param>
        /// <returns></returns>
        public T GenerateUIControl<T, T1>(Action<T1> action)
            where T :MVCUIBase
            where T1 : ControlSettingsBase
        {
            T1 local = Activator.CreateInstance<T1>();
            action(local);

            T t = (T)Activator.CreateInstance(typeof(T), local,_context,_viewDataContainer);
            return t;
        }

        
        private ViewContext _context;
        private IViewDataContainer _viewDataContainer;
        public UIEx(ViewContext context,IViewDataContainer viewDataContainer)
        {
            _context = context;
            _viewDataContainer = viewDataContainer;
        }
        #region 控件Help
        
        /// <summary>
        /// AutoComplete
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString AutoComplete(Action<AutoCompleteSettings> action)
        {
            return GenerateUIControl<AutoComplete, AutoCompleteSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// JqGrid
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString JqGrid(Action<GridSettings> action)
        {
            return GenerateUIControl<JqGrid, GridSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// button
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString Button(Action<ButtonSettings> action)
        {
            return GenerateUIControl<Button, ButtonSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 日期
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString Calendar(Action<CalendarSettings> action)
        {
            return GenerateUIControl<Calendar, CalendarSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 颜色面板
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString ColorPalette(Action<ColorPaletteSettings> action)
        {
            return GenerateUIControl<ColorPalette, ColorPaletteSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 颜色选择
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString ColorPicker(Action<ColorPickerSettings> action)
        {
            return GenerateUIControl<ColorPicker, ColorPickerSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// ComboBox
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString ComboBox(Action<ComboBoxSettings> action)
        {
            return GenerateUIControl<ComboBox, ComboBoxSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 右键菜单
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString ContextMenu(Action<ContextMenuSettings> action)
        {
            return GenerateUIControl<ContextMenu, ContextMenuSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 日期选择
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString DatePicker(Action<DatePickerSettings> action)
        {
            return GenerateUIControl<DatePicker, DatePickerSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 日期选择
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString DateTimePicker(Action<DateTimePickerSettings> action)
        {
            return GenerateUIControl<DateTimePicker, DateTimePickerSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 下拉列表
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString DropdownList(Action<DropdownListSettings> action)
        {
            return GenerateUIControl<DropDownList, DropdownListSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 颜色
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString FlatColorPicker(Action<FlatColorPickerSettings> action)
        {
            return GenerateUIControl<FlatColorPicker, FlatColorPickerSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString ListView(Action<ListViewSettings> action)
        {
            return GenerateUIControl<ListView, ListViewSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// MasketText
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString MaskedTextBox(Action<MaskedTextBoxSettings> action)
        {
            return GenerateUIControl<MaskedTextBox, MaskedTextBoxSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 菜单
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString Menu(Action<MenuSettings> action)
        {
            return GenerateUIControl<Menu, MenuSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 多选
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString MultiSelect(Action<MultiSelectSettings> action)
        {
            return GenerateUIControl<MultiSelect, MultiSelectSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 数字框
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString NumericTextBox(Action<NumericTextBoxSettings> action)
        {
            return GenerateUIControl<NumericTextBox, NumericTextBoxSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 面板
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString PanelBar(Action<PanelBarSettings> action)
        {
            return GenerateUIControl<PanelBar, PanelBarSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 进度条
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString ProgressBar(Action<ProgressBarSettings> action)
        {
            return GenerateUIControl<ProgressBar, ProgressBarSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString RangeSlider(Action<RangeSliderSettings> action)
        {
            return GenerateUIControl<RangeSlider, RangeSliderSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString Slider(Action<SliderSettings> action)
        {
            return GenerateUIControl<Slider, SliderSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString Splitter(Action<SplitterSettings> action)
        {
            return GenerateUIControl<Splitter, SplitterSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// Tab
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString TabStrip(Action<TabStripSettings> action)
        {
            return GenerateUIControl<TabStrip, TabStripSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString TimePicker(Action<TimePickerSettings> action)
        {
            return GenerateUIControl<TimePicker, TimePickerSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString ToolBar(Action<ToolBarSettings> action)
        {
            return GenerateUIControl<ToolBar, ToolBarSettings>(action).ToHtmlString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public MvcHtmlString Window(Action<WindowSettings> action)
        {
            return GenerateUIControl<Window, WindowSettings>(action).ToHtmlString();
        }

        #endregion
    }
}
